package com.ftd.DBConnection;

import com.ftd.model.LoginUser;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by apple on 16/8/4.
 */
public class DBConnection {
    public static Connection getConnection() {
        String url = "jdbc:mysql://localhost:3306/HiBook?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
        Connection connection = null;
        String name = "root";
        String password = "1";
        try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            connection = DriverManager.getConnection(url, name, password);


        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return connection;

    }

    public static void insert(String phonenumber) {

        Connection connection = getConnection();

        String sql = "INSERT INTO user(phonenumber) VALUES(?)";

        PreparedStatement preparedStatement = null;

        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, phonenumber);
            preparedStatement.execute();
            preparedStatement.close();
            connection.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public static List<LoginUser> select() {



        String sql = "SELECT phonenumber,star FROM user";
        List<LoginUser> userList = new ArrayList<LoginUser>();
        PreparedStatement preparedStatement;

        try {
            preparedStatement = getConnection().prepareStatement(sql);

            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                LoginUser loginUser = new LoginUser();

                loginUser.setPhonenumber(resultSet.getString("phonenumber"));

                loginUser.setStar(resultSet.getString("star"));

                userList.add(loginUser);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return userList;

    }

    public static void update(String phonenumber, String star,String n) {

        List users = select();
        String star1 = "";
        for (int i = 0; i < users.size(); i++) {
            LoginUser user = (LoginUser)users.get(i);
            if(user.getPhonenumber().equals(phonenumber) ){
                star1 = user.getStar();
                break;
            }
        }
        System.out.println(star1);

        String sql = "update user set star="+star1+","+"\'"+"star"+n+"\'"+":"+"\'"+star+"\'"+" where phonenumber="+phonenumber;
        System.out.println(sql);
        Connection conn = null;
        Statement st = null;
        try {
            //从工具类中获取连接
            conn = DBConnection.getConnection();
            st = conn.createStatement();
            int result = st.executeUpdate(sql);
            //处理结果
            if(result>0){
                System.out.println("操作成功");
            }else{
                System.out.println("操作失败");
            }
            st.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }




    public static void main(String[] args) {
//        DBConnection.insert("15564140625","'star0:'1','star1':'5','star2':'7'");
//        DBConnection.insert("17862923858","'star0:'1','star1':'5','star2':'7'");

//        List users = DBConnection.select();
//        LoginUser user = (LoginUser)users.get(1);
//        String p = user.getStar();
//        System.out.print(p);
        DBConnection.update("17862923858","4","3");

    }
}


